{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Detect Capital"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: detectCapitalUse"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #检测大写字母"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>我们定义，在以下情况时，单词的大写用法是正确的：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>全部字母都是大写，比如 <code>\"USA\"</code> 。</li>\n",
    "\t<li>单词中所有字母都不是大写，比如 <code>\"leetcode\"</code> 。</li>\n",
    "\t<li>如果单词不只含有一个字母，只有首字母大写，&nbsp;比如&nbsp;<code>\"Google\"</code> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给你一个字符串 <code>word</code> 。如果大写用法正确，返回 <code>true</code> ；否则，返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>word = \"USA\"\n",
    "<strong>输出：</strong>true\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>word = \"FlaG\"\n",
    "<strong>输出：</strong>false\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= word.length &lt;= 100</code></li>\n",
    "\t<li><code>word</code> 由小写和大写英文字母组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [detect-capital](https://leetcode.cn/problems/detect-capital/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [detect-capital](https://leetcode.cn/problems/detect-capital/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"USA\"', '\"FlaG\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        n = 0\n",
    "\n",
    "        for i in range(len(word)):\n",
    "            if word[i] == word[i].upper():\n",
    "                n += 1\n",
    "        if len(word) == 1:\n",
    "            return bool(1)\n",
    "        elif len(word) > 1 and n == len(word):\n",
    "            return bool(1)\n",
    "        elif len(word) > 1 and n == 1 and word[0] == word[0].upper():\n",
    "            return bool(1)\n",
    "        elif len(word) > 1 and n == 0:\n",
    "            return bool(1)\n",
    "        else:\n",
    "            return bool(0)\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "      return True if word.isupper() or word.islower() or word.istitle() else False\n",
    "\n",
    "      # import re\n",
    "      # pattern = r'^[A-Z]+$|^[a-z]+$|^[A-Z][a-z]+$'\n",
    "      # return True if re.match(pattern, word) else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word == word.upper() or word == word.lower() or word == word.title()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.lower() == word or word[0].lower() + word[1:] == word.lower() or word.upper() == word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        # strA = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n",
    "        # strB = strA.lower()\n",
    "\n",
    "        sumword = 0\n",
    "        for ch in word:\n",
    "            # if ch in strA:\n",
    "            if ch.isupper():\n",
    "                sumword += 2\n",
    "            # elif ch in strB:\n",
    "            elif ch.islower():\n",
    "                sumword += 1\n",
    "        \n",
    "        if sumword == 2 * len(word) or sumword == len(word):\n",
    "            return True\n",
    "        \n",
    "        # if sumword == len(word) + 1 and word[0] in strA:\n",
    "        if sumword == len(word) + 1 and word[0].isupper():\n",
    "            return True\n",
    "        \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        flag1 = 0\n",
    "        flag2 = 0\n",
    "        flag3 = 0\n",
    "        flag4 = 0\n",
    "        strlen = len(word)\n",
    "        for i in word:\n",
    "            if 64 < ord(i) < 91:\n",
    "                if not flag3:\n",
    "                    flag4+=1\n",
    "                flag1 += 1\n",
    "            else:\n",
    "                flag2 += 1\n",
    "            flag3 += 1\n",
    "        return strlen == flag1 or strlen == flag2 or (flag4 == 1 and flag2 == strlen-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.isupper():\n",
    "            return True\n",
    "        elif word.islower():\n",
    "            return True\n",
    "        elif word[0].isupper() and word[1:].islower():\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.istitle() or word.islower() or word.isupper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.upper()==word:\n",
    "            return True\n",
    "        n=0\n",
    "        for i in range(len(word)):\n",
    "            if word[i].upper()==word[i]:\n",
    "                n+=1\n",
    "        if n==0:\n",
    "            return True\n",
    "        else:\n",
    "            if word[0].upper()==word[0] and n==1:\n",
    "                return True\n",
    "            \n",
    "            else:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.upper() == word or word.lower() == word or word.capitalize() == word\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.lower() == word or word.upper() == word:\n",
    "            return True\n",
    "        if word[0:1].upper() == word[0:1] and word[1:].lower() == word[1:]:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.istitle() or word.islower() or word.isupper():\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if len(word) >= 2 and word[0].islower() and word[1].isupper():\n",
    "            return False\n",
    "        return all(word[i].islower() == word[1].islower() for i in range(2, len(word)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.istitle() or word.islower() or word.isupper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word in {word.upper(),word.lower(),word.capitalize()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        flag1 = 0\n",
    "        flag2 = 0\n",
    "        flag3 = 0\n",
    "        flag4 = 0\n",
    "        strlen = len(word)\n",
    "        for i in word:\n",
    "            if 64 < ord(i) < 91:\n",
    "                if not flag3:\n",
    "                    flag4+=1\n",
    "                flag1 += 1\n",
    "            else:\n",
    "                flag2 += 1\n",
    "            flag3 += 1\n",
    "        return strlen == flag1 or strlen == flag2 or (flag4 == 1 and flag2 == strlen-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word[0] == word[0].lower():\n",
    "            d = set(word)\n",
    "            for i in d:\n",
    "                if 'A' <= i <= 'Z':\n",
    "                    return False\n",
    "            return True\n",
    "        else:\n",
    "            flag1 = 0\n",
    "            flag2 = 0\n",
    "            d = set(word[1::])\n",
    "            for i in d:\n",
    "                if 'A' <= i <='Z':\n",
    "                    flag1 = 1\n",
    "                if 'a' <= i <= 'z':\n",
    "                    flag2 = 1\n",
    "                if flag1 == 1 and flag2 == 1:\n",
    "                    return False\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        count1=0\n",
    "        for i in word:\n",
    "            if 64<ord(i)<91:\n",
    "                count1+=1\n",
    "        if count1==0  or count1==len(word):\n",
    "            return True\n",
    "        elif count1==1:\n",
    "            if 64<ord(word[0])<91:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word == word.upper() or word == word.capitalize() or word == word.lower():\n",
    "            return True\n",
    "            \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if (len(word) == 1 or word == word.lower() or word == word.upper() or word == word.capitalize()):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if len(word)==1:\n",
    "            return True\n",
    "        if word[0]>=\"a\" and word[0]<=\"z\":\n",
    "            for i in range(1,len(word)):\n",
    "                if word[i]<\"a\" or word[i]>\"z\":\n",
    "                    return False\n",
    "        elif \"A\"<=word[0]<=\"Z\" and \"A\"<=word[1]<=\"Z\":\n",
    "            for i in range(2,len(word)):\n",
    "                if word[i]<\"A\" or word[i]>\"Z\":\n",
    "                    return False\n",
    "        elif \"A\"<=word[0]<=\"Z\" and \"a\"<=word[1]<=\"z\":\n",
    "            for i in range(2,len(word)):\n",
    "                if word[i]<\"a\" or word[i]>\"z\":\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        flag = 0\n",
    "        if len(word)==1 and word[0].isupper():\n",
    "            flag = 1\n",
    "        if word[0].isupper() and word[1:].isupper():\n",
    "            flag = 1\n",
    "        if word[0].isupper() and word[1:].islower():\n",
    "            flag = 1\n",
    "        if len(word)==1 and word[0].islower():\n",
    "            flag = 1 \n",
    "        if word[0].islower() and word[1:].islower():\n",
    "            flag = 1\n",
    "\n",
    "        return flag == 1\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.isupper() | word.islower() | word.istitle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        upp = 0\n",
    "        low = 0\n",
    "        n = len(word)\n",
    "\n",
    "        for i, ch in enumerate(word):\n",
    "            if ord(\"a\") <= ord(ch) <= ord(\"z\"):\n",
    "                if upp > 1:\n",
    "                    return False\n",
    "                low += 1\n",
    "            elif ord(\"A\") <= ord(ch) <= ord(\"Z\"):\n",
    "                if upp and low or ( not upp and i!=0):\n",
    "                    return False\n",
    "                upp += 1\n",
    "        if n==upp or n==low or (upp==1 and ord(\"A\") <= ord(word[0]) <= ord(\"Z\")):\n",
    "            return True\n",
    "        return False\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.lower() == word:\n",
    "            return True\n",
    "        elif word.upper() == word:\n",
    "            return True\n",
    "        else:\n",
    "            x = word[0].lower() + word[1:]\n",
    "            if x == word.lower():\n",
    "                return True\n",
    "            else:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.isupper() or word.islower() or word.istitle():\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word[0] == word[0].lower():\n",
    "            d = set(word)\n",
    "            for i in d:\n",
    "                if 'A' <= i <= 'Z':\n",
    "                    return False\n",
    "            return True\n",
    "        else:\n",
    "            flag1 = 0\n",
    "            flag2 = 0\n",
    "            for i in word[1::]:\n",
    "                if 'A' <= i <='Z':\n",
    "                    flag1 = 1\n",
    "                if 'a' <= i <= 'z':\n",
    "                    flag2 = 1\n",
    "                if flag1 == 1 and flag2 == 1:\n",
    "                    return False\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import string\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        lower = string.ascii_lowercase\n",
    "        upper = string.ascii_uppercase\n",
    "\n",
    "        if word[0] in lower:\n",
    "            for i in word[1:]:\n",
    "                if i in upper:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        else:\n",
    "            if len(word) == 1:\n",
    "                return True\n",
    "            status = word[1] in upper\n",
    "\n",
    "            if status:\n",
    "                for i in word[1:]:\n",
    "                    if i in lower:\n",
    "                        return False\n",
    "                return True\n",
    "            else:\n",
    "                for i in word[1:]:\n",
    "                    if i in upper:\n",
    "                        return False\n",
    "                return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        n=len(word)\n",
    "        s=word\n",
    "        if n==1:\n",
    "            return True\n",
    "        else:\n",
    "            a=s[1::]\n",
    "            if s[0].upper()==s[0]:\n",
    "                if (a.lower()==a)or(a.upper()==a):\n",
    "                    return True\n",
    "            if s.lower()==s:\n",
    "                    return True\n",
    "            else:\n",
    "                return False\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word == word.upper():\n",
    "            return True\n",
    "        if word == word.lower():\n",
    "            return True\n",
    "        if word[0].isupper() and word[1:].islower():\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def detectCapitalUse(self, word: str) -> bool:\r\n",
    "        if(len(word) == 1):\r\n",
    "            return True\r\n",
    "        if ord(word[0]) >= 65 and ord(word[0]) <= 90:\r\n",
    "            if ord(word[1]) >= 65 and ord(word[1]) <= 90:\r\n",
    "                for c in word[2:]:\r\n",
    "                    if ord(c) > 90 or ord(c) < 65:\r\n",
    "                        return False\r\n",
    "                return True\r\n",
    "            else:\r\n",
    "                for c in word[2:]:\r\n",
    "                    if ord(c) < 97 or ord(c) > 122:\r\n",
    "                        return False\r\n",
    "                return True\r\n",
    "        else:\r\n",
    "            for c in word[1:]:\r\n",
    "                if ord(c) < 97 or ord(c) > 122:\r\n",
    "                    return False\r\n",
    "            return True\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.isupper() or word.islower() or (word[0].isupper() and word[1:].islower())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        word_list = []\n",
    "        for w in word:\n",
    "            if w.isupper():\n",
    "                word_list.append(1)\n",
    "            else:\n",
    "                word_list.append(0)\n",
    "        first_flag = word_list[0]\n",
    "        if first_flag == 1:\n",
    "            if word_list[1:] == [1 for i in range(1, len(word_list))] or word_list[1:] == [0 for i in\n",
    "                                                                                           range(1, len(word_list))]:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        else:\n",
    "            if word_list[1:] == [0 for i in range(1, len(word_list))]:\n",
    "                return True\n",
    "            else:\n",
    "                return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if str.islower(word[0]):\n",
    "            return str.islower(word)\n",
    "        if len(word) <= 2:\n",
    "            return True\n",
    "        if str.islower(word[1]):\n",
    "            return str.islower(word[2:])\n",
    "        return str.isupper(word)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.upper() == word or word[1:].lower() == word[1:]\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word[0] == word[0].lower():\n",
    "            d = set(word)\n",
    "            for i in d:\n",
    "                if 'A' <= i <= 'Z':\n",
    "                    return False\n",
    "            return True\n",
    "        else:\n",
    "            flag1 = 0\n",
    "            flag2 = 0\n",
    "            for i in word[1::]:\n",
    "                if 'A' <= i <='Z':\n",
    "                    flag1 = 1\n",
    "                if 'a' <= i <= 'z':\n",
    "                    flag2 = 1\n",
    "                if flag1 == 1 and flag2 == 1:\n",
    "                    return False\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        a = \"QWERTYUIOPLKJHGFDSAZXCVBNM\"\n",
    "        b = \"qwertyuioplkjhgfdsazxcvbnm\"\n",
    "        c = 0\n",
    "        d = 0\n",
    "        e = 0\n",
    "        for i in range(len(word)):\n",
    "            if word[i] in a:\n",
    "                c += 1\n",
    "                e = i\n",
    "            elif word[i] in b:\n",
    "                d += 1\n",
    "        if c == len(word) or d == len(word) or (c == 1 and e == 0):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.isupper() == True:\n",
    "            return True\n",
    "        elif word.islower() == True:\n",
    "            return True\n",
    "        elif word[0].isupper() == True:\n",
    "            a = word[0].swapcase()\n",
    "            b = word.replace(word[0],a,1)\n",
    "            if b.islower() == True:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        else:\n",
    "             return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import string\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        lower = string.ascii_lowercase\n",
    "        upper = string.ascii_uppercase\n",
    "\n",
    "        if word[0] in lower:\n",
    "            for i in word[1:]:\n",
    "                if i in upper:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        else:\n",
    "            if len(word) == 1:\n",
    "                return True\n",
    "            status = word[1] in upper\n",
    "\n",
    "            if status:\n",
    "                for i in word[1:]:\n",
    "                    if i in lower:\n",
    "                        return False\n",
    "                return True\n",
    "            else:\n",
    "                for i in word[1:]:\n",
    "                    if i in upper:\n",
    "                        return False\n",
    "                return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.isupper():\n",
    "            return True\n",
    "        elif word.islower():\n",
    "            return True\n",
    "        elif word[0].isupper() and word[1:].islower():\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.isupper() or word.islower() or word == word.capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        a=word.lower()\n",
    "        b=word.upper()\n",
    "        c=word[0].upper()+word[1:].lower()\n",
    "        if word==a or word==b or word==c:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.isupper() or word.islower() or word.istitle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.isupper() or word.istitle() or word.islower():\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        #大写 65~90 小写 97~122\n",
    "        res=[]\n",
    "        if len(word)==1:\n",
    "            return True\n",
    "        for i in range(len(word)):\n",
    "            if ord(word[i])-ord('A')>=0 and ord(word[i])-ord('A')<26:\n",
    "                res.append(1)\n",
    "            else:\n",
    "                res.append(0)\n",
    "        if len(set(res))==1:\n",
    "            return True\n",
    "        if all(res[i]==0 if i!=0 else 1 for i in range(len(res))):\n",
    "            return True \n",
    "        return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if (len(word) == 1 or word == word.lower() or word == word.upper() or word == word.capitalize()):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        a = \"QWERTYUIOPLKJHGFDSAZXCVBNM\"\n",
    "        b = \"qwertyuioplkjhgfdsazxcvbnm\"\n",
    "        c = 0\n",
    "        d = 0\n",
    "        e = 0\n",
    "        for i in range(len(word)):\n",
    "            if word[i] in a:\n",
    "                c += 1\n",
    "                e = i\n",
    "            elif word[i] in b:\n",
    "                d += 1\n",
    "        if c == len(word) or d == len(word) or (c == 1 and e == 0):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        result=[]\n",
    "        for x in word:\n",
    "            if x.isupper():\n",
    "                result.append(1)\n",
    "            else:\n",
    "                result.append(0)\n",
    "        \n",
    "        sum_result=sum(result)\n",
    "        if sum_result==len(result) :\n",
    "            return True\n",
    "        elif sum_result==0:\n",
    "            return True\n",
    "        elif sum_result==1 and result[0]==1:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if len(word)==1:\n",
    "            return True\n",
    "        if word[1].isupper():\n",
    "            if word[0].islower():\n",
    "                return False\n",
    "            for i in range(2,len(word)):\n",
    "                if word[i].islower():\n",
    "                    return False\n",
    "            return True\n",
    "        else:\n",
    "            for i in range(2,len(word)):\n",
    "                if word[i].isupper():\n",
    "                    return False\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.isupper() or word.islower() or (word[0].isupper() and word[1:].islower()):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        upp = 0\n",
    "        low = 0\n",
    "        n = len(word)\n",
    "\n",
    "        for i, ch in enumerate(word):\n",
    "            if ord(\"a\") <= ord(ch) <= ord(\"z\"):\n",
    "                if upp > 1:\n",
    "                    return False\n",
    "                low += 1\n",
    "            elif ord(\"A\") <= ord(ch) <= ord(\"Z\"):\n",
    "                if upp and low:\n",
    "                    return False\n",
    "                upp += 1\n",
    "        if n==upp or n==low or (upp==1 and ord(\"A\") <= ord(word[0]) <= ord(\"Z\")):\n",
    "            return True\n",
    "        return False\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        all_upper = all(char.isupper() for char in word)\n",
    "        any_upper = not any(char.isupper() for char in word)\n",
    "        list_char = [char.isupper() for char in word]\n",
    "        count_upper = sum(1 for i in list_char if i)\n",
    "        true_count = (count_upper == 1 and word[0].isupper() == True)\n",
    "        return all_upper or any_upper or true_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if str.islower(word[0]):\n",
    "            return str.islower(word)\n",
    "        if len(word) <= 2:\n",
    "            return True\n",
    "        if str.islower(word[1]):\n",
    "            return str.islower(word[2:])\n",
    "        return str.isupper(word)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        upper_count = sum(1 for c in word if c.isupper())\n",
    "        if upper_count == len(word) or upper_count == 0 or (upper_count == 1 and word[0].isupper()):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word[0].isupper():\n",
    "            if word.isupper() or word[1:].islower():\n",
    "                return True \n",
    "            else:\n",
    "                return False \n",
    "        else:\n",
    "            if word.islower():\n",
    "                return True \n",
    "            else:\n",
    "                return False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if len(word)==2:\n",
    "            if ord(word[0])>=97 and ord(word[1])<=90:\n",
    "                return False\n",
    "            else :\n",
    "                return True\n",
    "        flag=0\n",
    "        if 65<=ord(word[0])<=90:\n",
    "            flag=1\n",
    "        word= word.replace(word[0],\"\",1)\n",
    "        count=0\n",
    "        for i in word:\n",
    "            k=ord(i)\n",
    "            if 97<=k<=122:\n",
    "                count+=0   #small\n",
    "            elif 65<=k<=90:\n",
    "                count+=1   #big\n",
    "        if (count==0  )or (count==len(word) and flag==1):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if str.islower(word[0]):\n",
    "            return str.islower(word)\n",
    "        if len(word) <= 2:\n",
    "            return True\n",
    "        if str.islower(word[1]):\n",
    "            return str.islower(word[2:])\n",
    "        return str.isupper(word)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.isupper() or word.islower() or word.istitle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.isupper() or word.islower() or word.istitle():\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        # 若第 1 个字母为小写，则需额外判断第 2 个字母是否为小写\n",
    "        if len(word) >= 2 and word[0].islower() and word[1].isupper():\n",
    "            return False\n",
    "        if len(word) < 2:\n",
    "            return True\n",
    "        l = word[1].islower()\n",
    "        # 无论第 1 个字母是否大写，其他字母必须与第 2 个字母的大小写相同\n",
    "        return all(c.islower() == l for c in word[2:])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        # O(n),O(1)\n",
    "        if len(word) <= 1:\n",
    "            return True\n",
    "\n",
    "        if word.isupper() or word[1:].islower():\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        s_list = []\n",
    "        for i in word:\n",
    "            if i >= \"A\" and i <= \"Z\":\n",
    "                s_list.append(i)\n",
    "        count = len(s_list)\n",
    "        if count == len(word):\n",
    "            return True\n",
    "        elif count == 0:\n",
    "            return True\n",
    "        elif count == 1 and word[0] in s_list:\n",
    "            if s_list[0] >= \"A\" and s_list[0] <= \"Z\":\n",
    "                return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def detectCapitalUse(self, word: str) -> bool:\n",
    "#         if word.isupper():  # 全部字母都是大写\n",
    "#             return True\n",
    "#         elif word.islower():  # 单词中所有字母都不是大写\n",
    "#             return True\n",
    "#         elif word[0].isupper() and word[1:].islower():  # 只有首字母大写\n",
    "#             return True\n",
    "#         else:\n",
    "#             return False\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        n = len(word)\n",
    "        if n == 1:\n",
    "            return True\n",
    "        sign1 = False\n",
    "        sign2 = False\n",
    "        if word[0] < 'a':\n",
    "            sign1 = True\n",
    "        if word[1] < 'a':\n",
    "            sign2 = True\n",
    "        if not sign1 and sign2:\n",
    "            return False\n",
    "        if sign1:\n",
    "            if sign2:  # 全部大写\n",
    "                for i in range(2, n):\n",
    "                    if word[i] >= 'a':\n",
    "                        return False\n",
    "            else:     # 首字母大写\n",
    "                for i in range(2, n):\n",
    "                    if word[i] < 'a':\n",
    "                        return False\n",
    "        else:  # 全部小写\n",
    "            for i in range(2, n):\n",
    "                if word[i] < 'a':\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.isupper() or word.islower() or word.istitle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.isupper() or word.islower() or (word[0].isupper() and word[1:].islower())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if re.match(r'^[A-Z]{1}[a-z]*?$', word):\n",
    "            return True\n",
    "        elif re.match(r'^[A-Z]+$', word):\n",
    "            return True\n",
    "        elif re.match(r'^[a-z]+$', word):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        # O(n),O(1)\n",
    "        if len(word) <= 1:\n",
    "            return True\n",
    "\n",
    "        n = len(word)\n",
    "        if word[0].islower() and word[1].isupper():\n",
    "            return False\n",
    "        return all(word[i].islower() == word[1].islower() for i in range(2, n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.islower():\n",
    "            return True\n",
    "        elif word.isupper():\n",
    "            return True\n",
    "        elif word.istitle():\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        n = len(word)\n",
    "        if n == 1: return True\n",
    "        a, b = word[0], word[1]\n",
    "        if ord(a)-ord('A') <= 26 and ord(b)-ord('A')<=26:\n",
    "            for i in range(2,n):\n",
    "                if ord(word[i])-ord('A') > 26: return False\n",
    "        if ord(a)-ord('A') <= 26 and ord(b)-ord('A')>26:\n",
    "            for i in range(2,n):\n",
    "                if ord(word[i])-ord('A') < 26: return False\n",
    "        if ord(a)-ord('A') > 26:\n",
    "            for i in range(1,n):\n",
    "                if ord(word[i])-ord('A') < 26: return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word.upper()==word:return True\n",
    "        if word.lower()==word:return True\n",
    "        if word[0].upper()==word[0] and word[1:].lower()==word[1:]:return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        # 若第 1 个字母为小写，则需额外判断第 2 个字母是否为小写\n",
    "        if len(word) >= 2 and word[0].islower() and word[1].isupper():\n",
    "            return False\n",
    "        \n",
    "        # 无论第 1 个字母是否大写，其他字母必须与第 2 个字母的大小写相同\n",
    "        return all(word[i].islower() == word[1].islower() for i in range(2, len(word)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        ret = True\n",
    "        if word.isupper():\n",
    "            ret = True\n",
    "        elif word.islower():\n",
    "            ret = True\n",
    "        elif word[0:1].isupper() and word[1:len(word)].islower():\n",
    "            ret = True\n",
    "        else:\n",
    "            ret = False\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.isupper() or word.islower() or word.istitle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        n  = len(word)\n",
    "        if n<2:\n",
    "            return True\n",
    "\n",
    "        if word[0].islower() and word[1].isupper():\n",
    "            return False\n",
    "        \n",
    "        return all(word[i].islower()==word[1].islower() for i in range(2, n) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        bigflag= 0\n",
    "        smallflag = 0\n",
    "        for str1 in word:\n",
    "            if 97>ord(str1) >=65:\n",
    "                bigflag =bigflag + 1\n",
    "            if ord(str1)>=97:\n",
    "                smallflag =smallflag + 1\n",
    "        if bigflag == len(word) or smallflag == len(word):\n",
    "            return True\n",
    "        if 97>ord(word[0]) >=65 and smallflag == len(word)-1:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        return word.islower() or word.isupper() or word.istitle()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word[0].islower():\n",
    "            for i in word:\n",
    "                if i.isupper():\n",
    "                    return False\n",
    "            return True\n",
    "        else:\n",
    "            c=0\n",
    "            for i in word:\n",
    "                if i.isupper():\n",
    "                    c+=1\n",
    "            if c==1 or c==len(word):\n",
    "                return True\n",
    "            return False\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def detectCapitalUse(self, word: str) -> bool:\n",
    "        if word[1:]==word[1:].lower() or word==word.upper():\n",
    "            return True\n",
    "        return False    "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
